home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / pj64.arc / MPX.ASM < prev    next >
Assembly Source File  |  1988-12-16  |  5KB  |  127 lines

  1. ;*****************************************************************************
  2. ;
  3. ; This program fragment shows how the multiplex interrupt can be used to
  4. ; provide reliable interprocess communications in an MS-DOS environment.
  5. ; A single segment is assumed (i.e. a .COM file) throughout this fragment,
  6. ; so adjustments will be necessary in a multiple segment situation.
  7. ;
  8. ; Keith Spitz / CH2M HILL Inc. / May 4, 1988
  9. ;
  10. ;*****************************************************************************
  11.  
  12. ;
  13. ; Define the segmentation.
  14. ;
  15. CODE        SEGMENT                ; A single segment.
  16.         ASSUME    CS:CODE , DS:CODE    ; Everything goes in it.
  17.  
  18. ;
  19. ; Program Data.
  20. ;
  21. MyPIDHeader    DB    'MYPID'            ; Helps me find MYPID if
  22.                         ; patching is necessary.
  23. MyPID        DB    0C0h            ; Assigned multiplex number.
  24. MPXAddr        DD    ?            ; Address of original multi-
  25.                         ; plex interrupt vector.
  26.  
  27. ;
  28. ; See if this process is already installed.  If so, this program becomes a
  29. ; client.  If not, this program installs itself and becomes the multiplex
  30. ; handler.
  31. ;
  32. CheckInstalled:    mov    ah , MyPID        ; PID in AH.
  33.         mov    al , 0            ; Get Installed State function.
  34.         call    MPXInt            ; Do the interrupt.
  35.         cmp    al , 0            ; Not there but OK to install?
  36.         je    DoInstall        ; Yes - become handler.
  37.         cmp    al , 1            ; Not there and NOT OK to inst?
  38.         je    CantInstall        ; Yes - bomb out.
  39.         cmp    al , 0FFh        ; Already installed?
  40.         je    AlreadyInstalled    ; Yes - become client.
  41.         jmp    Hmmmm            ; Hmmm.  Bomb out, I guess.
  42.  
  43. ;
  44. ; At this point, the program needs to become the handler process.  It installs
  45. ; itself into the multiplex interrupt chain and then (presumably) becomes
  46. ; memory resident.
  47. ;
  48. DoInstall:    mov    ah , 35h        ; Get interrupt vector func.
  49.         mov    al , 2Fh        ; For the multiplex interrupt.
  50.         int    21h            ; Get vector.
  51.         mov    WORD PTR MPXAddr[0] , bx; Save offset of vector.
  52.         mov    WORD PTR MPXAddr[2] , es; Save segment of vector.
  53.         mov    ah , 25h        ; Set interrupt vector func.
  54.         mov    al , 2Fh        ; For the multiplex interrupt.
  55.         mov    dx , OFFSET MPXHandler    ; Point at handler routine.
  56.         int    21h            ; Set new vector.
  57.         ...                ; Off to do whatever...
  58.  
  59. ;
  60. ; If the program gets here, the program has decided it is a client process.
  61. ; It will perform a couple of multiplex interupt requests and then quit
  62. ; (or whatever).
  63. ;
  64. AlreadyInstalled:mov    ah , MyPID        ; PID in AH.
  65.         mov    al , <some function>    ; Some process func in AL.
  66.         call    MPXInt            ; Do the function.
  67.         ...                ; ... Do other things ...
  68.         mov    ah , MyPID        ; PID in AH again.
  69.         mov    al , <another function>    ; Another function in AL.
  70.         call    MPXInt            ; Do the second function.
  71.         ...                ; ... Do more other things ...
  72.  
  73. ;
  74. ; This is the multiplex interrupt handler.  It will handle only those
  75. ; requests appropriate for the current process and pass on requests not
  76. ; for this process.  Note that DS is probably not pointing to the right
  77. ; place at the onset.
  78. ;
  79. MPXHandler    PROC    FAR            ; This MUST be a FAR routine.
  80.         cmp    ah , CS: MyPID        ; Is this for us?
  81.         jne    MPXHandNext        ; No - chain to next handler.
  82.         cmp    al , 0F8h        ; Reserved function?
  83.         jae    MPXHandRet        ; Yes - return immediately.
  84.         cmp    al , 0            ; Get install state function?
  85.         je    MPXHandFunc0        ; Yes - handle it.
  86.         ...                ; Check for other funcs here.
  87.         mov    al , <Invalid function>    ; Invalid function.
  88.         jmp    MPXHandRet        ; All done.
  89. ; Handle the 'Get Install State' function.
  90. MPXHandFunc0:    mov    al , 0FFh        ; Yes, we are installed.
  91.         jmp    MPXHandRet        ; All done.
  92. ; Chain to the next handler, if there is one.
  93. MPXHandNext:    mov    ax , CS: WORD PTR MPXAddr[0] ; Offset of next in chain.
  94.         or    ax , CS: WORD PTR MPXAddr[2] ; OR in segment.
  95.         je    MPXHandRet        ; If 0, no one to chain to.
  96.         jmp    CS: MPXAddr        ; Else chain to next handler.
  97. ; Return from the interrupt.
  98. MPXHandRet:    iret                ; All done.
  99. MPXHandler    ENDP                ; End of handler procedure.
  100.  
  101. ;
  102. ; This routine is used to perform multiplex interrupts.  Direct INT 2Fh's
  103. ; should not be called since it is possible that the 2Fh interrupt vector
  104. ; has not been initialized in some versions of DOS.
  105. ;
  106. MPXInt        PROC    NEAR            ; Start of procedure.
  107.         push    ax            ; Save AX, BX, and ES ...
  108.         push    bx            ; ... while they are used ...
  109.         push    es            ; ... to see if INT 2Fh is OK.
  110.         mov    ah , 35h        ; Get interrupt vector func.
  111.         mov    al , 2Fh        ; For the multiplex interrupt.
  112.         int    21h            ; Get vector.
  113.         mov    ax , es            ; Get the vector segment.
  114.         or    ax , bx            ; Or in the offset.
  115.         pop    es            ; Restore registers now...
  116.         pop    bx            ; ... (leaving flags alone ...
  117.         pop    ax            ; ... for later testing).
  118.         jz    MPXIntRet        ; If addr was 0, no vector!
  119.         int    2Fh            ; Otherwise OK to do INT 2Fh.
  120. MPXIntRet:    ret                ; All done.
  121. MPXInt        ENDP                ; End of procedure.
  122.  
  123. ;
  124. ; This is the end.
  125. ;
  126. CODE        ENDS
  127.